package com.haisen.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.haisen.core.utils.PageResultSet;
import com.haisen.core.utils.Result;
import com.haisen.dataobject.dto.RankDto;
import com.haisen.dataobject.entity.Rank;
import com.haisen.dataobject.query.RankQuery;
import com.haisen.service.DicService;
import com.haisen.service.RankServcie;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Controller
@RequestMapping("/rank")
@Slf4j
public class RankController {

    @Autowired
    private RankServcie rankServcie;

    @Autowired
    private DicService dicService;

    @GetMapping
    @RequiresPermissions("rank:view")
    public String pager(Model model){
        setCommonData(model);
        return "produce/rank";
    }



    @ResponseBody
    @RequiresPermissions("rank:create")
    @PostMapping("/create")
    public Result create(@Valid Rank rank){
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        rank.setCoperator(username);
        rankServcie.create(rank);
        return Result.success();
    }


    @ResponseBody
    @RequiresPermissions("rank:update")
    @PostMapping("/update")
    public Result update(@Validated Rank userScores){
        rankServcie.update(userScores);
        return Result.success();
    }

    @ResponseBody
    @RequiresPermissions("rank:delete")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long[] ids){
        rankServcie.delete(ids);
        return Result.success();
    }


    /**
     * 清单页面@RequestParam("username") String username,
     */
    @ResponseBody
    @RequestMapping("/detail")
    @RequiresPermissions("rank:view")
    public Result listDetail(HttpServletRequest request){
        String ajaxGet = request.getParameter("rankQuery");
        log.info("ajax传递对象参数={}",ajaxGet);
        RankQuery rankQuery =  JSON.parseObject(ajaxGet,RankQuery.class);
        return Result.success(rankServcie.find(rankQuery));
    }


    @ResponseBody
    @RequestMapping("/jsondetail")
    @RequiresPermissions("rank:view")
    public Object listJsonDetail(HttpServletRequest request) {
        /*String ajaxGet = request.getParameter("rankQuery");
        log.info("ajax传递对象参数={}",ajaxGet);
        RankQuery rankQuery =  JSON.parseObject(ajaxGet,RankQuery.class);*/
        // RankQuery rankQuery = new RankQuery();
        String jsonstr ="{total: 5,rows:[{\n" +
                "        \"id\": 0,\n" +
                "        \"name\": \"Item 0\",\n" +
                "        \"price\": \"$0\",\n" +
                "        \"amount\": 3\n" +
                "    }]}";

        Object object = JSON.parse(jsonstr);
        return object;
    }



    /**
     * 个人中心查看自己的积分加载的页面
     * @param model
     * @return
     */
    @GetMapping("/myrank")
    @RequiresPermissions("rank:view")
    public String pagerDetail(Model model){
        //setCommonData(model);
        return "produce/rankdetail";
       //return "base/content-wrapper";
    }


    @ResponseBody
    @RequestMapping("/rankdetail")
    @RequiresPermissions("rank:view")
    public PageResultSet<Rank> listDetail(@RequestParam("name") String nickName,RankQuery rankQuery,HttpServletRequest request){
        log.info("排名传递参数name={}",nickName);
        log.info("bootstrap-table ajax 查询参数 = {}",JSON.toJSONString(rankQuery));
        //url传递参数,个人中心查看的积分
         if (!StringUtils.isEmpty(nickName)){
             rankQuery.setUsername(nickName);
         }
         //如果从总积分过来查看的
        String ajaxParam = request.getParameter("rankQuery");
         //log.info("sevlet 传递参数={}",ajaxParam);
        if (!StringUtils.isEmpty(ajaxParam)){
            rankQuery =  JSON.parseObject(ajaxParam,RankQuery.class);
        }
        return rankServcie.findDetail(rankQuery);
    };

   /* public Result listDetail(@RequestParam("name") String nickName){
       // log.info("ajax传递对象参数={}",nickName);
        RankQuery rankQuery = new RankQuery();
        rankQuery.setUsername(nickName);
        return Result.success(rankServcie.find(rankQuery));
    }*/


    /**
     * 显示积分汇总页面
     * @param rankQuery
     * @return
     */
    @ResponseBody
    @RequestMapping("/summary")
    @RequiresPermissions("rank:view")
    public PageResultSet<RankDto> list(RankQuery rankQuery) {
        return rankServcie.findSummary(rankQuery);
    }

    /**
     * 导入积分
     * @param pathFile
     * @return
     */
    @ResponseBody
    @RequiresPermissions("rank:import")
    @PostMapping("/import")
    public Result importExcel(@RequestParam("importpathfile") String pathFile) {
        log.info("导入文件：", pathFile);
        rankServcie.importExcel2DataBase(pathFile);
        return Result.success();
    }




    private void setCommonData(Model model) {

       model.addAttribute("itemTypeMap",dicService.GetDic("bs_rank","item_type"));
    }

}
